
import { getFileAndFolder, addDownLoadTask } from '../../api/index.js'
import { dateFormat } from '../../utils/index.js'
Page({
  data() {
    return {
      loading: false,
      panType: 1,
      fileAndFolderId: '',
      btnStyle: {
        'border-radius': '20px',
        'height': '40px',
        'font-size': '18px',
        'line-height': '40px',
        'color': '#FFFFFF',
        'text-shadow': '0px 1px 2px rgba(0, 0, 0, .1)',
        'border': 'none',
        'width': '30%',
        'background': 'linear-gradient( 90deg, #5EA2F0 0%, #3069DC 100%)'
      },
      backBtnStyle:{
        'margin-right':'10px',
        'border-radius': '20px',
        'height': '40px',
        'font-size': '18px',
        'line-height': '40px',
        'color': '#666',
        'text-shadow': '0px 1px 2px rgba(0, 0, 0, .1)',
        'border': '1px solid #666',
        'width': '30%',
        'background': '#fff'
      },
      pathes: [
        { name: '我的应用 / 多云管家', id: '',parentId:"",parentIdName:'' }],
      customList: [],
      btnloading:false,
      panSelect:0
    }
  },
  methods: {
    pathClick(val) {

      let pathes = this.pathes
      let cur = pathes[pathes.length - 1]
      if (cur.id == val.id) {
        return
      }

      this.fileAndFolderId = val.id
      while (true) {
        let cur = pathes.pop()
        // 说明这是最后一个
        if (cur.id == val.id) {
          pathes.push(cur)
          break
        }
      }
      this.getFileAndFolder()
    },
    gotoTaskView() {
      if(this.btnloading) return
      // "optType": "10"  文件操作
      // "optType": "20"  目录操作
      let pathes = this.pathes
      let optInfos = []
      this.btnloading = true
      ok.getH5AppStorage({
        key: 'downFiles'
      }).then(res => {
        optInfos = res.data.data.map(val => {
          let file = {
            "optFileId": val.id,
            "optFileName": val.name,
            "optParentFileFolderId": val.parentId,
            "optParentFileFolderName": val.parentIdName,
            "optType": !val.fileType ? "20" : "10"
          }
          if(val.fileType){
            file.size = val.size
          }
          return file
        })
        let cur = pathes[pathes.length - 1]
        
        let distInfo = {
          "optDiskType": this.panType,
          "distDiskType": 1, // 目标盘:默认是天翼云盘
          "distFile": cur.id,
          "distFileName":cur.id === '' ? '' : cur.name,
          "parentDistFile":cur.parentId,
          "parentDistFileName": cur.parentIdName,
          panSelect:this.panSelect,
        }

        console.log(optInfos,distInfo)


        addDownLoadTask({
          optInfos,
          distInfo
        }).then(res => {
          if (res.data.code === 500&&res.data.msg==='用户登录已超时，请重新登录') {
            ok.clearH5AppStorage().then(res=>{
              this.$router.replace('/pages/login/index')
            })
            .catch(err=> {
              console.log("接口调用异常",err);
            });
            return
          }
          if(res.data.code===0){

            this.$Modal.confirm({
              title: '任务创建成功',
              content: '关闭小程序不影响传输任务，且可到任务中心查看最新进度情况',
              submitText: '去查看',
              cancelText: '知道了',
              onCancel: () => {
                console.log('cancel')
                this.$router.replace({ path: '/pages/info/index' })
              },
              onSubmit: () => {
                this.$router.replace({ path: '/pages/download/index' })
              }
            })
            
          }else{
            ok.showToast(res.data.msg)
          }
          this.btnloading = false
        })
      }).catch(err => {
        this.btnloading = false
        console.log("接口调用异常", err);
      });
      
    },
    gotoBack(){
      this.$router.replace({ path: '/pages/info/index' })
    },
    async getFileAndFolder() {
      this.loading = true
      const { data } = await getFileAndFolder({
        panType: 1,
        fileAndFolderId: this.fileAndFolderId
      })

      if (data.code === 500&&data.msg==='用户登录已超时，请重新登录') {
        ok.clearH5AppStorage().then(res=>{
          this.$router.replace('/pages/login/index')
        })
        .catch(err=> {
          console.log("接口调用异常",err);
        });
        return
      }
      this.loading = false
      let folderList = data.data.folderList.map(val => {
        val.checkValue = false
        val.time = val.updateTime
        return val
      })
      this.customList = [...folderList]
    },
    clickFolder(item) {
      console.log(item)
      this.fileAndFolderId = item.id
      this.pathes.push({
        id: item.id,
        name: item.name,
        parentId:item.parentId,
        parentIdName:item.parentIdName
      })
      this.getFileAndFolder()
    },
  },
  mounted() {
    this.getFileAndFolder()

    ok.getH5AppStorage({
      key: 'panType'
    }).then(res => {
      this.panType = res.data.data
    }).catch(err => {
      console.log("接口调用异常", err);
    });

    ok.getH5AppStorage({
      key: 'panSelect'
    }).then(res => {
      this.panSelect = res.data.data
    }).catch(err => {
      console.log("接口调用异常", err);
    });
  }
});
